package com.gxa.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultDTO<T> {
    private Integer code;
    private String msg;
    private Map<String, T> data = new HashMap<>();

    public ResultDTO addData(String key, T data) {
        this.getData().put(key, data);
        return this;
    }

    public ResultDTO fromStatus(Status status) {
        this.setCode(status.getCode());
        this.setMsg(status.getMsg());
        return new ResultDTO(status.getCode(), status.getMsg(), null);
    }

    public ResultDTO<T> fromStatus(Status status, T data) {
        this.setCode(status.getCode());
        this.setMsg(status.getMsg());
        this.addData("data", data);
        HashMap<String, T> newData = new HashMap<>();
        newData.put("data", data);
        return new ResultDTO(status.getCode(), status.getMsg(), new HashMap(newData));
    }

    public static ResultDTO ok() {
        return new ResultDTO(Status.SUCCESS.getCode(), Status.SUCCESS.getMsg(), null);
    }

    public static ResultDTO ok(String msg) {
        return new ResultDTO(Status.SUCCESS.getCode(), msg, null);
    }

    public static ResultDTO failure() {
        return new ResultDTO(Status.SYSTEM_ERROR.getCode(), Status.SYSTEM_ERROR.getMsg(), null);
    }

    public static ResultDTO failure(String msg) {
        return new ResultDTO(Status.SYSTEM_ERROR.getCode(), msg, null);
    }

    public static ResultDTO failure(Status status) {
        return new ResultDTO(status.getCode(), status.getMsg(), null);
    }
}
